//: Playground - noun: a place where people can play

import Cocoa

//有返回值
func runoob(site : String) ->String{
    return (site)
}

var site = runoob(site: "你们好")
//返回值元组类型,求最小最大值
func runoob1(arr : [Int]) ->(min : Int , max : Int){
    var min = arr[0]
    var max = arr[0]
    for (index , _) in arr.enumerated() {
        if min > arr[index] {
            min = arr[index]
        }
        if max < arr[index] {
            max = arr[index]
        }
    }
    return (min,max)
}

var site1 = runoob1(arr: [2,4,1,10,8,22,3])

//无返回值
func runoob2(site : String){

}
//局部参数名site
func runoob3(site : String){
    
}
//外部参数名name
func runoob4(name a: String){
    print(a)
}
print(runoob4(name: "jianghu"))

//可变参数
func vari<N>(members: N...){
    for obj in members {
        print(obj)
    }
}
vari(members: 1,2,2,3)
//常量，变量及 I/O 参数
//常量
func swapTwoInts(_ a: Int,_ b: Int){
    
}
//变量
func swapTwoInts1(_ a:inout Int,_ b:inout Int){
    let t = a
    a = b
    b = t
}
var x = 4
var y = 5
swapTwoInts1(&x, &y)
print(x,y)

//使用函数类型
func funxType(a : Int,b: Int) {
    
}

var funx1 : (Int,Int) ->() = funxType

print("输出结果：\(funx1(2,3))")

//函数类型作为参数类型、函数类型作为返回类型
func sum(a: Int, b: Int) -> Int {
    return a + b
}
var addition: (Int, Int) -> Int = sum
print("输出结果: \(addition(40, 89))")

func another(addition: (Int, Int) -> Int, a: Int, b: Int) {
    print("输出结果: \(addition(a, b))")
}
another(addition: sum, a: 10, b: 20)

//函数嵌套
func calcDecrement(forDecrement total: Int) -> () -> Int {
    var overallDecrement = 0
    func decrementer() -> Int {
        overallDecrement -= total
        return overallDecrement
    }
    return decrementer
}
let decrem = calcDecrement(forDecrement: 30)
print(decrem())

//闭包类似block
let block = {(a:Int,b:Int) ->Int in
   return a + b
}
print(block(2,3))

//闭包表达式
let names = ["AT", "AE", "D", "S", "BE"]

// 使用普通函数(或内嵌函数)提供排序功能,闭包函数类型需为(String, String) -> Bool。
func backwards(s1: String, s2: String) -> Bool {
    return s1 > s2
}
var reversed = names.sorted(by: backwards)

print(reversed)























